import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';
import { VitePWA } from 'vite-plugin-pwa';
import viteCompression from 'vite-plugin-compression';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';

export default defineConfig({
  resolve: {
    extensions: ['.vue', '.ts', '.js', '.json'],
    alias: {
      '@': path.resolve(__dirname, '.', 'src'),
    },
  },
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    VitePWA({
      registerType: 'autoUpdate',
      devOptions: {
        enabled: true,
        type: 'module',
      },
      workbox: {
        runtimeCaching: [
          {
            urlPattern: /.*/,
            handler: 'StaleWhileRevalidate',
          },
        ],
        maximumFileSizeToCacheInBytes: 4 * 1024 * 1024, // 需要缓存的最大文件大小
      },
      manifest: {
        name: 'Tetris V3',
        short_name: 'Tetris V3',
        description:
          'A Tetris game, running on the web, responsive and installable.',
        display: 'standalone',
        theme_color: '#111',
        start_url: '/',
        icons: [
          {
            src: '/icon.png',
            type: 'image/png',
            sizes: '171x171',
          },
        ],
        orientation: 'any',
        background_color: '#fff',
        lang: 'en',
        categories: ['game'],
        screenshots: [
          {
            src: '/sssample.jpg',
            type: 'image/jpg',
            sizes: '414x736',
            form_factor: 'narrow',
          },
        ],
      },
    }),
    viteCompression({
      // 压缩
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz',
    }),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
      symbolId: 'icon-[dir]-[name]',
    }),
  ],
  server: {
    host: '0.0.0.0',
  },
  css: {
    postcss: {
      plugins: [tailwindcss, autoprefixer],
    },
  },
  build: {
    assetsInlineLimit: 0, // 原为4k 改为0 避免小音频base64化不进行打包
    rollupOptions: {
      // 打包后文件名不哈希化
      output: {
        assetFileNames: `assets/[name].[ext]`,
      },
    },
  },
  // 生产环境启动
  // esbuild: { // 去除console和debugger
  //   drop: ['console', 'debugger']
  // },
});
